import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
// import {anyRoute, asyncRoutes, constantRoutes} from '../../../../shopbgs/src/router/routes'
// import router from '../../../../shopbgs/src/router'
//
// function filterAsyncRoutes(asyncRoutes, routeNames) {
//   const accessedRoutes = asyncRoutes.filter(route => {
//
//     if (routeNames.includes(route.name)) {
//
//       if (route.children && route.children.length) {
//         //如果这个路由下面还有下一级的话,就递归调用
//         const cRoutes = filterAsyncRoutes(route.children, routeNames)
//         //如果过滤一圈后,没有子元素了,这个父级菜单就也不显示了
//         if (cRoutes && cRoutes.length>0) {
//           route.children = cRoutes
//           return true
//         }
//         return false
//       }
//       return true
//     }
//
//     return false
//   })
//
//   return accessedRoutes
// }


const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    buttons: [],
    routes: [], // 本用户所有的路由,包括了固定的路由和下面的addRouters
    asyncRoutes: [] // 本用户的角色赋予的新增的动态路由
  }
}

const state = getDefaultState()

const mutations = {
  SET_USER: (state, userInfo) => {
    state.name = userInfo.name // 用户名
    state.avatar = userInfo.avatar // 头像
    state.roles = userInfo.roles // 角色列表
    state.buttons = userInfo.buttons // 按钮权限列表
  },
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  // SET_ROUTES: (state, asyncRoutes) => {
  //   // 保存异步路由
  //   state.asyncRoutes = asyncRoutes
  //   // 合并常量路由,异步路由与备选路由, 并保存
  //   state.routes = constantRoutes.concat(asyncRoutes, anyRoute) //将固定路由和新增路由进行合并, 成为本用户最终的全部路由信息
  //   // 将当前用户的异步权限路由和备选路由添加到路由器
  //   router.addRoutes([...asyncRoutes, anyRoute])
  // }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        // const { name, avatar } = data
        //
        // commit('SET_NAME', name)
        // commit('SET_AVATAR', avatar)
        commit('SET_USER', data)
        // commit('SET_ROUTES', filterAsyncRoutes(data.routes))
        // commit('SET_ROUTES', filterAsyncRoutes(asyncRoutes, data.routes))
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

